package com.fenghuajueli.module_home.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.recycler.bindAdapter
import com.fenghuajueli.libbasecoreui.utils.MyStatusBarUtils
import com.fenghuajueli.module_home.R
import com.fenghuajueli.module_home.adapter.HomeVideoListRvAdapter
import com.fenghuajueli.module_home.databinding.ActivityHomeVideoListBinding
import com.fenghuajueli.module_home.databinding.ItemCourseListBinding
import com.fenghuajueli.module_home.model.HomeViewModel
import com.fenghuajueli.module_home.room.homevideo.HomeVideo
import com.fenghuajueli.module_home.utils.startPlayVideo
import com.ijianji.module_play_video.model.PlayVideoModel
class HomeVideoListActivity : AppCompatActivity() {

    lateinit var binding: ActivityHomeVideoListBinding

    companion object {
        fun start(context: Context, type: String, title: String) {
            context.startActivity(
                Intent(
                    context,
                    HomeVideoListActivity::class.java
                ).putExtra("type", type).putExtra("title", title)
            )
        }

    }

    private val model: HomeViewModel by viewModels()

    private val models = PlayVideoModel()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHomeVideoListBinding.inflate(layoutInflater)
        setContentView(binding.root)
        MyStatusBarUtils.setStatusBar(this, "#00fff000")
        val title = intent.getStringExtra("title")!!
        val type = intent.getStringExtra("type")!!
        binding.apply {
            myActionBar2.tv_title.text = title
            rvHome.layoutManager = LinearLayoutManager(this@HomeVideoListActivity)
            if (type == "首页") {
                initHomeAdapter(title)
            } else {
                initCourseAdapter(title)
            }
        }
    }

    private fun initHomeAdapter(title: String) {
        if(title == "自然拼读"){
            models.loadVideoList(this, 1,"bilibili_BV1PS4y1Y73r", 4)
            models.videoListData.observe(this){
                if (it.size<127){
                    ToastUtils.showShort("播单错误，请联系客服")
                    return@observe
                }
                val list = listOf(it[0],it[26],it[56],it[84],it[125])
                model.homeVideoDao.getHomeVideoByType(title).observe(this@HomeVideoListActivity) { it1 ->
                    binding.rvHome.adapter = HomeVideoListRvAdapter(it1.onEachIndexed { index, homeVideo ->
                        homeVideo.itemVid = list[index].vid
                    },title)
                }
            }
            return
        }
        binding.apply {
            model.homeVideoDao.getHomeVideoByType(title).observe(this@HomeVideoListActivity) {
                rvHome.adapter = HomeVideoListRvAdapter(it,title)
            }
        }
    }

    private fun initCourseAdapter(title: String) {
        val adapter =  binding.rvHome.bindAdapter<HomeVideo, ItemCourseListBinding> {
            onBindView { itemViewHolder, itemData, position ->
                itemViewHolder.apply {
                    when(title){
                        "英语考试" -> ivImg.setImageResource(R.mipmap.img_yyks_list)
                        "口语练习" -> ivImg.setImageResource(R.mipmap.img_kyttl_list)
                        "职场英语" -> ivImg.setImageResource(R.mipmap.img_zcyy_list)
                        else -> ivImg.setImageResource(R.mipmap.img_jcyy_list)
                    }
                    tvTitle.text = title
                    tvHint.text = itemData.name
                }
                onItemClick { itemViewHolder, itemData, position ->
                    startPlayVideo(itemData.vid,3)
                }
            }
        }
        model.homeVideoDao.getHomeVideoByType(if (title == "口语练习")"口语天天练" else title).observe(this@HomeVideoListActivity) {
            adapter.submitList(it)
        }
    }
}